/////////////////////////////////////////////////////////////////////////
// MIT License
//
// Copyright (c) 2000-2011 OpenNETCF Consulting, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  
////////////////////////////////////////////////////////////////////////

using System;

namespace OpenNETCF.Windows.Forms.Calendar
{
	/// <summary>
	/// CalendarItem used in AgendView, MonthView or WeekView calendar control
	/// </summary>
	public class CalendarItem: IComparable<CalendarItem>
	{
		private DateTime m_date;
		private TimeSpan m_duration;
        private string m_text;
		private object m_dataObj;

        /// <summary>
        /// Default constructor
        /// </summary>
		public CalendarItem(): this(DateTime.Now, DateTime.Now.AddHours(1))
		{
			m_date = DateTime.Now;
			m_duration = new TimeSpan(0, 1, 0);
            m_text = "";
		}

        /// <summary>
        /// Overloaded constructor
        /// </summary>
        /// <param name="dateStart"></param>
        /// <param name="duration"></param>
        /// <param name="dataObject"></param>
        /// <param name="text"></param>
		public CalendarItem(DateTime dateStart, TimeSpan duration, object dataObject, string text)
		{
			m_date = dateStart;
			m_duration = duration;
			m_dataObj = dataObject;
            m_text = text;
        }

        /// <summary>
        /// Overloaded constructor
        /// </summary>
        /// <param name="dateStart"></param>
        /// <param name="duration"></param>
		public CalendarItem(DateTime dateStart, TimeSpan duration): this(dateStart, duration, null, "")
		{
		}

        /// <summary>
        /// Overloaded constructor
        /// </summary>
        /// <param name="dateStart"></param>
        /// <param name="dateEnd"></param>
        /// <param name="dataObject"></param>
		public CalendarItem(DateTime dateStart, DateTime dateEnd, object dataObject): this(dateStart, dateEnd.Subtract(dateStart), dataObject, "")
		{
        }

        /// <summary>
        /// Overloaded constructor
        /// </summary>
        /// <param name="dateStart"></param>
        /// <param name="dateEnd"></param>
		public CalendarItem(DateTime dateStart, DateTime dateEnd): this(dateStart, dateEnd, null)
		{
		}

        /// <summary>
        /// Gets or sets the start time of the calendar item
        /// </summary>
		public DateTime StartTime
		{
			get { return m_date; }
			set { m_date = value; }
		}

        /// <summary>
        /// Gets or sets the end time of the calendar item
        /// </summary>
		public DateTime EndTime
		{
			get { return m_date.Add(m_duration); }
			set 
			{ 
				if ( value < m_date )
					throw new ArgumentException("End date cannot precede start date");
				m_duration = value.Subtract(m_date);
			}
		}

        /// <summary>
        /// Gets or sets the text of the calendar item
        /// </summary>
        public string Text
        {
            get { return m_text; }
            set { m_text = value; }
        }

        /// <summary>
        /// Gets or sets the data associated with the calendar item
        /// </summary>
        public object DataObject
		{
			get { return m_dataObj; }
			set { m_dataObj = value; }
		}

        /// <summary>
        /// Gets or sets the durration of the calendar item
        /// </summary>
		public TimeSpan Duration
		{
			get { return m_duration; }
			set { m_duration = value; }
		}

        /// <summary>
        /// Determines whether two calender items overlap eachother
        /// </summary>
        /// <param name="anotherItem"></param>
        /// <returns></returns>
		public bool Overlaps(CalendarItem anotherItem)
		{
			return ( anotherItem.StartTime >= StartTime && anotherItem.StartTime <= EndTime ) || ( StartTime >= anotherItem.StartTime && StartTime <= anotherItem.EndTime );
		}

        #region IComparable<CalendarItem> Members

        /// <summary>
        /// COmpares two calendar items
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int CompareTo(CalendarItem other)
        {
            if (other == null) throw new ArgumentNullException("other");
            int i = StartTime.CompareTo(other.StartTime);
            if (i != 0) return i;
            i = EndTime.CompareTo(other.EndTime);
            if (i != 0) return i;
            return string.Compare(Text, other.Text);
        }

        #endregion
    }
}
